//
//  PHWelcomeViewController.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/22.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHWelcomeViewController: UIViewController {
    
    
    override func loadView() {
        view = backImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    // MARK: - 设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //更新约束来达到动画效果
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "welcome")
                })
        }
    }
    
    func setupUI() {
        //添加子控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
    }
    
    
    
    // MARK: - 懒加载子控件
    private lazy var backImageView :UIImageView = UIImageView(imgName: "ad_background")
    
    private lazy var headImageView :UIImageView = {
       let headImg = UIImageView(imgName: "avatar_default_big")
        headImg.ph_setImage((PHUserAccountView.sharedTools.userAccount?.avatar_large)!, placeholderImageName: "avatar_default_big")
        
        headImg.layer.borderWidth = 2
        headImg.layer.borderColor = themeColor.CGColor
        headImg.layer.cornerRadius = 45
        headImg.layer.masksToBounds = true
        return headImg
        
    }()
    
    private lazy var messageLabel:UILabel = {
       let msgLabel = UILabel()
        msgLabel.textColor = UIColor.darkGrayColor()
        msgLabel.text = "欢迎回来"
        msgLabel.font = UIFont.systemFontOfSize(18)
        msgLabel.textAlignment = .Center
        msgLabel.alpha = 0
        
        return msgLabel
    }()
}







